﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ServiceModel;
using DotNetNuke.Common;
using System.ServiceModel.Description;
using System.Security.Policy;
using iLangl.Transport;

namespace iLangl.Customer.DNN
{
	public class PushServiceManager
	{

		#region Singelton Pattern


		private static PushServiceManager instance;
		private static object mutex = new object();
		/// <summary>
		/// The instance of the PushServiceManager.
		/// </summary>
		public static PushServiceManager Instance
		{
			get
			{
				if (instance == null)
				{
					lock (mutex)
					{
						if (instance == null)
						{
							instance = new PushServiceManager();

						}
					}
				}
				return instance;
			}
		}


		#endregion
		~PushServiceManager()
		{
			Close();
		}


		ServiceHost pushservice = null;
		public void Initialize()
		{
			if (pushservice != null)
				return;

			intializeService();
		}

		private void intializeService()
		{
			//Uri baseAddress = new Uri(GetUrl());
			//pushservice = new ServiceHost(typeof(PushService), baseAddress);

			//ServiceMetadataBehavior smb = new ServiceMetadataBehavior();

			//smb.HttpGetEnabled = true;
			//smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
			//pushservice.Description.Behaviors.Add(smb);

			//EnableDebug(pushservice);
			//pushservice.AddServiceEndpoint(typeof(ITransport), new BasicHttpBinding(), baseAddress);

			//pushservice.Faulted += new EventHandler(pushservice_Faulted);
		}

		private void EnableDebug(ServiceHost sh)
		{
			ServiceDebugBehavior debug = sh.Description.Behaviors.Find<ServiceDebugBehavior>();
			if (debug == null)
			{
				sh.Description.Behaviors.Add(
					new ServiceDebugBehavior() { IncludeExceptionDetailInFaults = true });
			}
			else
			{
				// make sure setting is turned ON     
				if (!debug.IncludeExceptionDetailInFaults) { debug.IncludeExceptionDetailInFaults = true; }
			}
		}

		void pushservice_Faulted(object sender, EventArgs e)
		{
			pushservice.Abort();
			pushservice.Faulted -= new EventHandler(pushservice_Faulted);
			intializeService();
			Open();

		}

		public void Open()
		{
			if ((pushservice.State == CommunicationState.Opened) || (pushservice.State == CommunicationState.Opening))
				return;



			pushservice.Open();
		}

		public void Close()
		{
			if ((pushservice.State == CommunicationState.Closed) || (pushservice.State == CommunicationState.Closing))
				return;

			if (pushservice != null)
			{
				try
				{
					pushservice.Close();
				}
				catch
				{
					try
					{
						pushservice.Abort();
					}
					catch { }
				}
				pushservice = null;
			}
		}


		public string GetUrl()
		{
			var Request = HttpContext.Current.Request;
			string uri = string.Format("{0}://{1}", Request.Url.Scheme, Request.Url.Authority);
			return uri + "/iLanglPushService";
		}
	}
}